package com.stedu.mall.userservice.service;

import com.stedu.mall.common.bean.Addr;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.AddrService;
import com.stedu.mall.userservice.mapper.AddrMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@DubboService
public class AddrServiceImpl implements AddrService {
    @Autowired
    private AddrMapper addrMapper;
    @Override
    public int insert(Addr addr) {
        addr.setStatus(0);
        return addrMapper.insert(addr);
    }

    @Override
    public boolean delete(Integer id, Integer userId) throws SteduException {
        //判断地址是否存在
        Addr addr = addrMapper.selectById(id);
        if (addr==null){
            throw new SteduException("该地址不存在，无法删除");
        }
        //判断是否属于当前的用户
        if (!addr.getUserId().equals(userId)){
            throw new SteduException("该地址属于其他用户，无法删除");
        }
        //删除
        return addrMapper.delete(id)==1;
    }

    @Override
    public Addr selectById(Integer id) {
        return addrMapper.selectById(id);
    }

    @Override
    public boolean update(Addr addr, Integer userId) throws SteduException {
        //判断地址是否存在
        Addr a = addrMapper.selectById(addr.getId());
        if (a==null){
            throw new SteduException("该地址不存在，无法修改");
        }
        //判断是否属于当前的用户
        if (!a.getUserId().equals(userId)){
            throw new SteduException("该地址属于其他用户，无法修改");
        }
        //如果设置为默认地址的处理 --如果将某个地址设为默认地址，则将之前默认地址修改为非默认地址
        if(Integer.valueOf(1).equals(addr.getStatus())){
            //查出当前的默认地址
            Addr condition = new Addr();
            condition.setStatus(1);
            List<Addr> addrList = addrMapper.selectByCondition(condition);
            //将当前的地址设置为普通地址
            if (addrList.size()>0){
                Addr a1 = new Addr();
                a1.setId(addrList.get(0).getId());
                a1.setStatus(0);
                addrMapper.update(a1);
            }
        }
        return addrMapper.update(addr)==1;
    }

    @Override
    public List<Addr> selectByCondition(Addr condition) {
        return addrMapper.selectByCondition(condition);
    }
}
